package com.cqie.secondhandtransaction.controller;

import com.cqie.secondhandtransaction.DTO.UserDTO;
import com.cqie.secondhandtransaction.model.Result;
import com.cqie.secondhandtransaction.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gdy
 * @since 2025-02-10
 */
@RestController
@Api(value = "用户管理", tags = {"用户管理"})
public class UserInfoController extends BaseController{
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/user/update")
    @ApiOperation(value = "修改个人信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<Void> updateUserInfo(@ModelAttribute @Validated UserDTO userInfo, @RequestParam(value = "picture", required = false) MultipartFile picture) {
        userInfoService.updateUserInfo(userInfo, picture);
        return response();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    @PreAuthorize("hasRole('ROLE_ADMIN')")//管理员权限
    public Result<Void> deleteUserInfo(@RequestParam(name = "id") Long id) {
        userInfoService.deleteUserInfo(id);
        return response();
    }

}
